home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / mplus_1.exe / MENUDEMO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-01  |  16.9 KB  |  611 lines

  1. /*--------------------------------------------------------------
  2.  *  File:           MENUDEMO.C
  3.  *  Description:    Demo the MPLUS Menu System. 
  4.  *
  5.  *  This demo developed for the MPLUS Graphic Interface Library.
  6.  *-------------------------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <graph.h>
  10. #include <math.h>
  11.  
  12. #include "gplus.h"
  13. #include "gscreen.h"
  14. #include "mouser.h"
  15. #include "mpmenu.h"
  16.  
  17. #define ESC     0x011B          /* scan & ascii code for ESC key */
  18. #define CTRL_M   0x320D         /* hot key for memory */
  19.  
  20. int dummy(), exitfun();
  21. int memfun();
  22. int mres(), hres(), eres(), vres();
  23. int info(), demo1(), demo2();
  24.  
  25. void sinplot();
  26. void setaxis();
  27.  
  28. /*--------------------------------------------------------------
  29.  *  Initialize menu structures.
  30.  *-------------------------------------------------------------*/
  31.  
  32. /*      Titles for pull down menus.  Main menu bar done last because
  33.  *      of backward referencing.
  34.  */
  35.  
  36. struct MENU_ITEM mi_file[] =
  37. {
  38.     "Save", dummy, NULL, 0, 0,1,0,      /* dummy entries marked */
  39.     "Load", dummy, NULL, 0, 0,1,0,      /*   with grey out */
  40.     "DOS",  dummy, NULL, 0, 0,1,0,
  41.     "Exit to System", exitfun, NULL, 0, 0,0,0,
  42.     NULL, NULL, NULL, 0,0,0,
  43. };
  44.  
  45. struct MENU_ITEM mi_system[] =
  46. {
  47.     "Memory\tCTRL+M", memfun, NULL, CTRL_M, 0,0,0,
  48.     NULL, NULL, NULL, 0, 0,0,0,
  49. };
  50.  
  51. struct MENU_ITEM mi_videomode[] = 
  52. {
  53.     "Medium Res 4 Color", mres, NULL, 0, 0,0,0,
  54.     "Hi Res Black & White", hres, NULL, 0, 0,0,0,
  55.     "EGA", eres, NULL, 0, 0,0,0,
  56.     "VGA", vres, NULL, 0, 0,0,0,
  57.     NULL, NULL, NULL, 0, 0,0,0,
  58. };
  59.  
  60. struct MENU_ITEM mi_help[] = 
  61. {
  62.     "Info", info, NULL, 0, 0,0,0,
  63.     "Demo 1", demo1, NULL, 0, 0,0,0,
  64.     "Demo 2", demo2, NULL, 0, 0,0,0,
  65.     NULL, NULL, NULL, 0, 0,0,0,
  66. };
  67.  
  68. /*      Titles for main menu goes last because of backward
  69.  *      referencing of pulldown menu structures.
  70.  */
  71. struct MENU_ITEM menubar[] =
  72. {
  73.     "File", NULL, mi_file, 0, 1, 0, 0,    
  74.     "System", NULL, mi_system, 0, 1, 0, 0, 
  75.     "Video", NULL, mi_videomode, 0, 1, 0, 0,
  76.     "Help", NULL, mi_help, 0, 1, 0, 0,
  77.     "Exit", exitfun, NULL, 0, 0, 0, 0,
  78.     NULL, NULL, NULL, 0,0,0,
  79. };
  80.     
  81. struct MENU_INFO menu_info = 
  82. {
  83.     ESC, exitfun,                           /* exit key & function */
  84.     BLACK, CYAN, RED, GREY,                 /* attr of bar menu */
  85.     _GBORDER, BLUE, WHITE, BLUE, GREY,      /* attr of pull down windows */
  86. };
  87.  
  88. extern struct videoconfig _videoconfig;
  89.  
  90. /*--------------------------------------------------------------
  91.  *  Function:       main
  92.  *  Description:    demos the bar menu.
  93.  *  Return value:   0 returned to parent process.
  94.  *--------------------------------------------------------------*/
  95. main()
  96. {
  97.     int ret;
  98.     int (*funptr)();
  99.     char ms_flag;
  100.  
  101.     /*      Try to set video mode to EGA
  102.      */
  103.     if( mpsetvideomode( _ERESCOLOR ) )
  104.         mi_videomode[2].checkmark = 1;      /* check off EGA mode in menu */
  105.     else if( mpsetvideomode( _HRESBW ) )
  106.         mi_videomode[1].checkmark = 1;      /* check of HRES in menu */
  107.     else
  108.     {
  109.         printf("\nUnrecognized video hardware.\n");
  110.         exit(1);
  111.     }
  112.  
  113.     /*      Open menu bar with colors defined in menu_info.
  114.      */
  115.     mb_open( 1, 1, 80, &menu_info, menubar );
  116.  
  117.     ms_flag = ms_reset();
  118.     ms_setevent(1);
  119.     ms_showcursor();
  120.  
  121.     if( ms_flag == 0 )
  122.     {
  123.         gdialog( GDINFORM, GDOKAY );
  124.         gdwrite( "No mouse detected but keyboard is\n" );
  125.         gdwrite( "supported.  Press \"O\" to quit dialogue\n");
  126.         gdwrite( "box, then press ALT+first letter of\n");
  127.         gdwrite( "menu title.  See chapter 6.\n");
  128.         gdprompt();
  129.         gdclose();
  130.     }
  131.  
  132.     funptr = NULL;
  133.  
  134.     while( funptr != exitfun || ret != 0)
  135.     {
  136.         funptr = mb_run();
  137.         if ( funptr != NULL)
  138.             ret = funptr();             /* execute selected function */
  139.     }
  140.     mb_close();
  141.     ms_setevent(0);
  142.     mpsetvideomode( _DEFAULTMODE );
  143.     return 0;
  144. }
  145. /*--------------------------------------------------------------
  146.  *  Function:       dummy
  147.  *  Description:    Dummy function to invoke from bar menu
  148.  *  Return value:   0
  149.  *--------------------------------------------------------------*/
  150. int dummy()
  151. {
  152.     gdialog( GDINFORM, GDOKAY );
  153.     gdwrite("Function not available.");
  154.     gdprompt();
  155.     gdclose();
  156.     return 0;
  157. }
  158. /*--------------------------------------------------------------
  159.  *  Function:       exitfun
  160.  *  Description:    quit this program
  161.  *  Return value:   0
  162.  *--------------------------------------------------------------*/
  163. int exitfun()
  164. {
  165.     int i;
  166.  
  167.     gdialog( GDWARN, GDYESNO );
  168.     gdwrite( "Quit bar menu demo?");
  169.     i = gdprompt();
  170.     gdclose();
  171.  
  172.     return i;
  173. }
  174. /*--------------------------------------------------------------
  175.  *  Function:       memfun
  176.  *  Description:    Display memory info to dialog box
  177.  *  Return value:   0
  178.  *--------------------------------------------------------------*/
  179. int memfun()
  180. {
  181.     char buffer[41];
  182.  
  183.     gdialog( GDINFORM, GDOKAY );
  184.  
  185.     sprintf( buffer, "Memory available: %u bytes\n", _memavl() );
  186.     gdwrite( buffer );
  187.     sprintf( buffer, "Max contiguous block: %u bytes\n", _memmax() );
  188.     gdwrite( buffer );
  189.  
  190.     gdprompt();
  191.     gdclose();
  192.  
  193.     return 0;
  194. }
  195. /*--------------------------------------------------------------
  196.  *  Function:       mres
  197.  *  Description:    Set the screen to medium resolution, 4 color
  198.  *  Return value:   0
  199.  *--------------------------------------------------------------*/
  200. int mres()
  201. {
  202.     char buffer[41];
  203.     int i;
  204.  
  205.     gdialog( GDINFORM, GDOKCAN );
  206.     gdwrite( "Reset mode to four color,\nmedium resolution?" );
  207.     i = gdprompt();
  208.     gdclose();
  209.  
  210.     if( i == 0 )
  211.     {
  212.         mb_close();
  213.         if( mpsetvideomode( _MRES4COLOR ) == 0 )
  214.         {
  215.             gdialog( GDERROR, GDOKAY );
  216.             gdwrite( "Video mode not supported by hardware." );
  217.             gdprompt();
  218.             gdclose();
  219.         }
  220.         else
  221.         {
  222.             /*      Select colors for palette 1
  223.              */
  224.             _selectpalette(1);
  225.  
  226.             menu_info.fg0 = 0x01;       /* cyan */
  227.             menu_info.bg0 = 0x02;       /* magenta */
  228.             menu_info.keycolor0 = 0x03; /* light grey */
  229.  
  230.             menu_info.fg = 0x01;        /* cyan */
  231.             menu_info.bg = 0x03;        /* light grey */
  232.             menu_info.keycolor = 0x01;
  233.  
  234.             menu_info.greyout0 = menu_info.greyout = 0x03;
  235.  
  236.             /*  all checkmarks off.  Check off first title in menu.
  237.              */
  238.             mb_setbits( mi_videomode, MB_CHECKMARK, 0 ); 
  239.             mi_videomode[0].checkmark = 1;
  240.  
  241.             mb_open( 1, 1, 80, &menu_info, menubar );
  242.             ms_showcursor();
  243.         }
  244.     }
  245.     
  246.     return 0;
  247. }
  248. /*--------------------------------------------------------------
  249.  *  Function:       hres
  250.  *  Description:    Set the screen to black and white hi res
  251.  *  Return value:   0
  252.  *--------------------------------------------------------------*/
  253. int hres()
  254. {
  255.     char buffer[41];
  256.     int i;
  257.  
  258.     gdialog( GDINFORM, GDOKCAN );
  259.     gdwrite( "Reset mode to black and white,\nhigh resolution?" );
  260.     i = gdprompt();
  261.     gdclose();
  262.  
  263.     if( i == 0 )
  264.     {
  265.         mb_close();
  266.         if( mpsetvideomode( _HRESBW ) == 0 )
  267.         {
  268.             gdialog( GDERROR, GDOKAY );
  269.             gdwrite( "Video mode not supported by hardware." );
  270.             gdprompt();
  271.             gdclose();
  272.         }
  273.         else
  274.         {
  275.             /*      Reset colors to black and white.
  276.              */
  277.             menu_info.fg0 = menu_info.fg = 0x00;
  278.             menu_info.bg0 = menu_info.bg = 0x07;
  279.             menu_info.keycolor0 = menu_info.keycolor = 0x07;
  280.             menu_info.greyout0 = menu_info.greyout = 0x07;
  281.  
  282.             /*  all checkmarks off.  Check off second title in menu.
  283.              */
  284.             mb_setbits( mi_videomode, MB_CHECKMARK, 0 ); 
  285.             mi_videomode[1].checkmark = 1;
  286.  
  287.             mb_open (1,1,80, &menu_info, menubar);
  288.  
  289.             ms_showcursor();
  290.         }
  291.     }
  292.     
  293.     return 0;
  294. }
  295. /*--------------------------------------------------------------
  296.  *  Function:       eres
  297.  *  Description:    Set the screen to ega, 16 color
  298.  *  Return value:   0
  299.  *--------------------------------------------------------------*/
  300. int eres()
  301. {
  302.     char buffer[41];
  303.     int i;
  304.  
  305.     gdialog( GDINFORM, GDOKCAN );
  306.     gdwrite( "Reset mode to ega color?" );
  307.     i = gdprompt();
  308.     gdclose();
  309.  
  310.     if( i == 0 )
  311.     {
  312.         /*      Microsoft is weird.  If we go from VGA to EGA, we get
  313.          *      43 line EGA.  Avoid this by going thru another graphics mode.
  314.          */
  315.         mb_close();
  316.         mpsetvideomode( _MRES4COLOR );
  317.         if( mpsetvideomode( _ERESCOLOR ) == 0 )
  318.         {
  319.             gdialog( GDERROR, GDOKAY );
  320.             gdwrite( "Video mode not supported by hardware." );
  321.             gdprompt();
  322.             gdclose();
  323.         }
  324.         else
  325.         {
  326.             menucolors( &menu_info );
  327.  
  328.             /*  all checkmarks off.  Check off third title in menu.
  329.              */
  330.             mb_setbits( mi_videomode, MB_CHECKMARK, 0 ); 
  331.             mi_videomode[2].checkmark = 1;
  332.  
  333.             mb_open (1,1,80, &menu_info, menubar);
  334.             ms_showcursor();
  335.         }
  336.     }
  337.     return 0;
  338. }
  339. /*--------------------------------------------------------------
  340.  *  Function:       vres
  341.  *  Description:    Set the screen to vga res 
  342.  *  Return value:   0
  343.  *--------------------------------------------------------------*/
  344. int vres()
  345. {
  346.     char buffer[41];
  347.     int i;
  348.  
  349.     gdialog( GDINFORM, GDOKCAN );
  350.     gdwrite( "Reset mode to vga color?" );
  351.     i = gdprompt();
  352.     gdclose();
  353.  
  354.     if( i == 0 )
  355.     {
  356.         mb_close();
  357.         if( mpsetvideomode( _VRES16COLOR ) == 0 )
  358.         {
  359.             gdialog( GDERROR, GDOKAY );
  360.             gdwrite( "Video mode not supported by hardware." );
  361.             gdprompt();
  362.             gdclose();
  363.         }
  364.         else
  365.         {
  366.             menucolors( &menu_info );
  367.  
  368.             /*  all checkmarks off.  Check off fourth title in menu.
  369.              */
  370.             mb_setbits( mi_videomode, MB_CHECKMARK, 0 ); 
  371.             mi_videomode[3].checkmark = 1;
  372.  
  373.             mb_open (1,1,80, &menu_info, menubar);
  374.             ms_showcursor();
  375.         }
  376.     }
  377.     
  378.     return 0;
  379. }
  380. /*--------------------------------------------------------------
  381.  *  Function:       info
  382.  *  Description:    info function invoked from bar menu
  383.  *  Return value:   0
  384.  *--------------------------------------------------------------*/
  385. int info()
  386. {
  387.     int device;
  388.     int ch;
  389.     struct ms_status ms_status;
  390.     GWDW *gwptr;
  391.  
  392.     if( _videoconfig.numtextcols <= 40 )
  393.     {
  394.         gdialog( GDINFORM, GDOKAY );
  395.         gdwrite( "Please change to\n");
  396.         gdwrite( "hi-res mode to\n");
  397.         gdwrite( "view info." );
  398.         gdprompt();
  399.         gdclose();
  400.         return 0;
  401.     }
  402.  
  403.     gwptr = gwdwtopen( 5, 10, 19, 65, _GBORDER, BRIGHTWHITE, GREEN );
  404.     if( (char *)gwptr == NULL )
  405.     {
  406.         gdialog( GDWARN, GDOKAY );
  407.         gdwrite( "Insufficient memory.\n");
  408.         gdwrite( "Set video mode to B/W Hi-res and\n");
  409.         gdwrite( "try again." );
  410.         gdprompt();
  411.         gdclose();
  412.         return 0;
  413.     }
  414.     outtext("         The MPLUS Graphic Interface Library\n", LIGHTYELLOW, -1);
  415.     outtext("         Copyright 1989-1991 by Michael Yam\n\n", LIGHTYELLOW, -1 );
  416.     outtext("MPLUS is a user supported program.  If you find this\n", BRIGHTWHITE, -1);
  417.     outtext("package useful, please register your copy by sending\n", BRIGHTWHITE, -1);
  418.     outtext("fifty dollars ($50) to:\n\n", BRIGHTWHITE, -1 );
  419.     outtext("     Michael Yam\n", BRIGHTWHITE, -1 );
  420.     outtext("     230 East 88th St.  #6B\n", BRIGHTWHITE, -1 );
  421.     outtext("     New York, NY  10128\n\n", BRIGHTWHITE, -1 );
  422.     outtext("Thank you for your support!   ", BRIGHTWHITE, -1 );
  423.     outtext("Press a key...", BLACK, -1 );
  424.     while(1)
  425.     {
  426.         device = dev_ready( &ch, &ms_status );
  427.         if( device == _KB )
  428.         {
  429.             if( ch != 0 )
  430.                 break;
  431.         }
  432.         if( device == _MS )
  433.         {
  434.             if( ms_status.rbtn || ms_status.lbtn )
  435.                 break;
  436.         }
  437.     }
  438.     gwdwclose( gwptr );
  439.     return 0;
  440. }
  441. /*--------------------------------------------------------------
  442.  *  Function:       demo1
  443.  *  Description:    plot a sine wave
  444.  *  Return value:   0
  445.  *--------------------------------------------------------------*/
  446. int demo1()
  447. {
  448.     extern struct videoconfig _videoconfig;
  449.  
  450.     short fg, bg, highlite;
  451.     int ch;
  452.     int device;
  453.     double pi;
  454.     struct ms_status ms_status;
  455.     GWDW *gwptr1, *gwptr2;
  456.  
  457.     /*      Although MPLUS is geared for EGA and VGA modes, try to
  458.      *      accomodate low res graphics.
  459.      */
  460.     if( _videoconfig.mode == _MRES4COLOR )
  461.     {
  462.         _selectpalette(1);
  463.         fg = 1;
  464.         bg = 2;
  465.         highlite = 3;
  466.     }
  467.     else if( _videoconfig.mode == _HRESBW )
  468.     {
  469.         fg = 7;
  470.         bg = 0;
  471.         highlite = 7;
  472.     }
  473.     else
  474.     {
  475.         fg = MAGENTA;
  476.         bg = LIGHTYELLOW;
  477.         highlite = BLUE;
  478.     }
  479.  
  480.     pi = 3.141592654;
  481.     gwptr1 = gwdwopen( 20, 20, 300, 150, _GBORDER, fg, bg );
  482.     _setcolor( highlite );
  483.     setaxis( gwptr1 );
  484.     sinplot( -2*pi, 2*pi, 280, 50 );
  485.  
  486.     gwptr2 = gwdwtopen( 22, 1, 24, 40, _GFILLINTERIOR, bg, fg );
  487.     outtext( "Press a key or mouse button...", highlite, -1 );
  488.  
  489.     while (1)
  490.     {
  491.         /*      Wait for keystroke or mouse buttons.
  492.          */
  493.         device = dev_ready( &ch, &ms_status );
  494.         if( device == _MS )
  495.         {
  496.             if( ms_status.lbtn || ms_status.rbtn )
  497.                 break;
  498.         }
  499.         else if( device == _KB )
  500.         {
  501.             if( ch != 0 )
  502.                 break;
  503.         }
  504.     }
  505.     gwdwclose( gwptr2 );
  506.     gwdwclose( gwptr1 );
  507.     return 0;
  508. }
  509. /*--------------------------------------------------------------
  510.  *  Function:       sinplot
  511.  *  Description:    plot a sine wave
  512.  *  Return value:   none
  513.  *--------------------------------------------------------------*/
  514. void sinplot( range1, range2, xpixels, ysf )
  515. double range1;
  516. double range2;
  517. short xpixels;                  /* x pixels available */
  518. int ysf;                        /* y scale factor */
  519. {
  520.     int i;
  521.     int cursor;
  522.     double numperxpix;
  523.     double xpixpernum;
  524.     double x, y;
  525.  
  526.     cursor = ms_cursor();
  527.     ms_hidecursor();
  528.     numperxpix = (fabs(range2-range1))/(double)xpixels;
  529.     xpixpernum = 1/numperxpix;
  530.  
  531.     /*      Calculate first point.  Position cursor with _moveto.
  532.      *      Adjust sign for y axis.
  533.      */
  534.     x = range1;
  535.     y = sin( x );
  536.     _moveto( (short)(range1/numperxpix), (short)(-( y*ysf )) );
  537.  
  538.     for( i=1; i<xpixels; ++i)
  539.     {
  540.         x += numperxpix;
  541.         y = sin( x );
  542.         _lineto( (short)(x * xpixpernum), (short)(-(y*ysf)) );
  543.     }
  544.     if( cursor )
  545.         ms_showcursor();
  546. }
  547. /*--------------------------------------------------------------
  548.  *  Function:       setaxis
  549.  *  Description:    draw axis and set logical origin to center
  550.  *                  of screen.
  551.  *  Return value:   none
  552.  *--------------------------------------------------------------*/
  553. void setaxis( gwptr )
  554. GWDW *gwptr;
  555. {
  556.     int cursor;
  557.     short xctr, yctr;
  558.  
  559.     cursor = ms_cursor();
  560.     ms_hidecursor();
  561.     xctr = (gwptr->x2-gwptr->x1)/2;
  562.     yctr = (gwptr->y2-gwptr->y1)/2;
  563.  
  564.     _moveto( xctr, 0 );
  565.     _lineto( xctr, gwptr->y2-gwptr->y1 );
  566.     _moveto( 0, yctr );
  567.     _lineto( gwptr->x2-gwptr->x1, yctr );
  568.  
  569.     gwdwsetorg( gwptr, xctr, yctr );
  570.     if( cursor )
  571.         ms_showcursor();
  572. }
  573. /*--------------------------------------------------------------
  574.  *  Function:       demo2
  575.  *  Description:    
  576.  *  Return value:   0
  577.  *--------------------------------------------------------------*/
  578. int demo2()
  579. {
  580.     gdialog( GDINFORM, GDOKAY );
  581.     gdwrite("This is it.");
  582.     gdprompt();
  583.     gdclose();
  584.  
  585.     return 0;
  586. }
  587. /*--------------------------------------------------------------
  588.  *  Function:       menucolors
  589.  *  Description:    Set MENU_INFO struct with standard colors.
  590.  *                  For EGA & VGA modes.  See also mb_stdcolors()
  591.  *                  in manual.
  592.  *  Return value:   0
  593.  *--------------------------------------------------------------*/
  594. int menucolors(menu_info)
  595. struct MENU_INFO *menu_info;
  596. {
  597.     menu_info->fg0 = BLACK;
  598.     menu_info->bg0 = CYAN;
  599.     menu_info->keycolor0 = RED;
  600.     menu_info->greyout0 = GREY;
  601.     menu_info->border = _GBORDER;
  602.     menu_info->fg = BLUE;
  603.     menu_info->bg = WHITE;
  604.     menu_info->keycolor = BLUE;
  605.     menu_info->greyout = GREY;
  606.     return 0;
  607. }
  608. /*-------------------------------------------------------------*
  609.  *                      End of MENUDEMO.C                      *
  610.  *-------------------------------------------------------------*/
  611.